
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form">
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary" data-page="page/set/admin_user_add.html"
                        data-click="common_add_click"  data-callback="render_select" data-box-h="450px" data-box-w="550px">
                    <i class="layui-icon layui-icon-add-1 layui-btn-icon"></i>新增用户</button>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary common-reload" >
                    <i class="layui-icon-refresh-3 layui-btn-icon layui-icon"></i>重新加载</button>
            </div>
            <div class="layui-input-inline" style="margin-left: 25px;width:400px;">
                <input type="text" name="key" required  placeholder="手机号或用户名，如果是数字，将按手机号搜索" class="layui-input">
            </div>
            <div class="layui-input-inline" id="common-clear-input" style="margin-left: -30px;cursor: pointer;margin-right: 5px;">
                <i class="layui-icon layui-icon-close"></i></div>
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary common-search"><i class="layui-icon layui-icon-search"></i></button>
            </div>
            <div class="layui-input-inline red" id="common-search-tips"></div>
        </div>
        <table id="user-table" class="layui-table" lay-filter="user-table"></table>
    </div>
</div>
<script type="text/html" id="user-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    tb = table.render({
        elem: '#user-table',
        url: '/admin/admin.htm',
        cols: [[
            {field: 'id', width: 80, title: 'ID'},
            {field: 'phone', width: 150, title: '手机号',templet:function(e){
                    if( search_key ){
                        return search_reg(search_key,e.phone);
                    }
                    return e.phone;
                }},
            {field: 'name',width:150, title: '姓名',templet:function(e){
                    if( search_key ){
                        return search_reg(search_key,e.name);
                    }
                    return e.name;
                }},
            {field: 'create_time', title: '注册时间',width:180,templet:function(d){
                    return util.timeAgo(d.create_time * 1000 )
                }},
            {field: 'last_login_time', width: 180, title: '最后登录',templet:function(e){
                    return util.timeAgo(e.last_login_time * 1000)
                }},
            {field:'g_name',title:'所属组',width:100,templet:function (e) {
                            return e.grp.name
                }},
            {field:'status',title:'状态',width:100,templet:function (e) {
                    return '<input type="checkbox" name="is_show" lay-filter="show" lay-skin="switch" lay-text="是|否" '+ (e.status === 'y' ? 'checked' : '')
                        +' value="'+e.id+'">'
                }},
            {title: '操作', width: 250, templet: '#user-state', align:"center"}
        ]]
    })
    table.on('tool(user-table)',function(e){
        const event = e.event;
        if( event === 'edit' ){
            http.getHtml('page/set/admin_user_add.html',function(res){
                window.openBox = layer.open({
                    type:1,
                    title:'修改用户【'+ e.data.name +'】',
                    area:['600px','500px'],
                    content:res,
                    success:function(ele){
                        $(ele).find('legend').text('修改用户');
                        layuimini.render($(ele),e.data);
                        render_select($(ele),e.data.group_id)

                    }
                })
            })
        }
        else if ( event === 'del')  {
            const id = e.data.id;
            layer.confirm('真的要删除吗',{icon:3},function(index){
                http.get('/admin/admin/del.htm?id='+id).done(function(res){
                    if(res.code === 200)
                    {
                        e.del();
                    }
                    layer.msg(res.msg);
                })
            })
        }
    })
    form.on('switch(show)',function(data){
        const id = data.value , status = data.elem.checked ? 'y' : 'n';
        http.post('/admin/admin/save.htm',{ data : { id:id,status:status } }).done(res => {
            layer.msg(res.msg);
        })
    });
    function render_select($elem,id = 0){
        http.get('/admin/group/admin.htm?page=1&limit=100').done(res => {
            let dom = ''
            const data = res.data
            if(data){
                data.map(function(v){
                    dom += '<option value="'+v.id+'"'+ (v.id === id ? ' selected' : '') +'>'+v.name+'</option>'
                })
            }
            $elem.find('select[name="group_id"]').html(dom);
            form.render('select')
        })
    }
</script>